package com.medusa.aps.business.modules.security.mp.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.aps.business.common.security.model.enums.Roles;
import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.security.model.input.RoleQueryPara;
import com.medusa.aps.business.modules.security.model.output.RoleUserVO;
import com.medusa.aps.business.modules.security.model.output.RoleVO;
import com.medusa.aps.business.modules.security.mp.entity.Role;
import com.medusa.aps.business.modules.security.mp.entity.RoleMenu;
import com.medusa.aps.business.modules.security.mp.entity.UserRole;
import com.medusa.aps.business.modules.security.mp.mapper.RoleMapper;
import com.medusa.aps.business.modules.security.mp.service.IRoleMenuService;
import com.medusa.aps.business.modules.security.mp.service.IRoleService;
import com.medusa.aps.business.modules.security.mp.service.IUserRoleService;
import com.medusa.aps.business.modules.security.mp.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xianhui.yu
 **/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private IRoleMenuService roleMenuService;
    @Resource
    private IUserRoleService userRoleService;
    @Resource
    private IUserService userService;

    /**
     * 系统管理-角色表 列表分页
     *
     * @param roleQueryPara 角色参数
     * @return 角色列表
     */
    @Override
    public Page<Role> listPage(RoleQueryPara roleQueryPara) {
        return baseMapper.listPage(roleQueryPara);
    }

    /**
     * 获取系统管理-角色表 信息
     *
     * @param roleId 角色id
     */
    @Override
    public RoleVO getRole(Integer roleId) {
        Role role = this.getById(roleId);
        RoleVO roleVO = new RoleVO();
        if (role == null) {
            return roleVO;
        }
        BeanUtils.copyProperties(role, roleVO);
        // 关联权限
        List<Long> menuIds = roleMenuService.lambdaQuery()
                .eq(RoleMenu::getRoleId, roleId)
                .list().stream()
                .map(RoleMenu::getMenuId)
                .toList();
        roleVO.setMenuIds(menuIds);

        // 关联成员
        List<UserRole> userRoles = userRoleService.lambdaQuery()
                .eq(UserRole::getRoleId, roleId)
                .list();
        if (CollectionUtils.isEmpty(userRoles)) {
            return roleVO;
        }
        roleVO.setRoleUserVOS(
                userService.listByIds(userRoles.stream().map(UserRole::getUserId).collect(Collectors.toSet()))
                        .stream()
                        .map(
                                user -> new RoleUserVO().setUserId(user.getId())
                                        .setJobNumber(user.getJobNumber())
                                        .setUserName(user.getUsername())
                        ).toList()
        );
        return roleVO;
    }

    /**
     * 获取所有角色
     *
     * @return 角色列表
     */
    @Override
    public List<RoleVO> selectAll() {
        return this.lambdaQuery()
                .select(Role::getId, Role::getName, Role::getValue)
                .eq(Role::getValue, Roles.SUPER_CUSTOM_ADMIN)
                .list()
                .stream()
                .map(role -> new RoleVO().setId(role.getId()).setName(role.getName()))
                .toList();
    }


}
